package com.learn.multi_thread.B_PracticeThread.Test04;

import java.util.Random;

public class RedEnvelopeThread extends Thread {
    static double money = 100;
    static int count = 3;
    static final double MIN = 0.01;

    @Override
    public void run() {
        synchronized (RedEnvelopeThread.class) {
            if (count == 0) {
                // 判断，共享数据是否到达了末尾，已经达到末尾
                System.out.println(getName() + "发现红包已抢完");
            } else {
                 // 判断，共享数据是否到了末尾（没有到末尾）
                // 定义一个变量，标识中奖金额
                double prize = 0;
                if (count == 1) {
                    prize = money;
                } else {
                    Random r = new Random();
                    double bounds = money - (count -1) * MIN;
                    prize = r.nextDouble();
                    if (prize < MIN) {
                        prize = MIN;
                    }
                }
                money = money - prize;
                count--;
                System.out.println(getName() + "抢到了"+prize+"元");
            }
        }
    }
}
